import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const billStore = createSlice({
  name: "bill",
  initialState: {
    billList: [],
  },
  reducers: {
    setBillList(state, actions) {
      state.billList = actions.payload;
    },
    // 同步添加账单方法
    addBill(state, action) {
      state.billList.push(action.payload);
    },
  },
});
const { setBillList,addBill } = billStore.actions;
const getBillList = () => {
  return async (dispatch) => {
    const res = await axios.get("http://localhost:8888/data");
    dispatch(setBillList(res.data));
  };
};
const addBillList = (data) => {
    return async (dispatch) => {
      // 编写异步请求
      const res = await axios.post('http://localhost:8888/data', data)
      // 触发同步reducer
      dispatch(addBill(res.data))
    }
  }
const reducer = billStore.reducer;
export { getBillList,addBillList };
export default reducer;
